package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

public class OrderDao extends ConnectionBase implements IOrderDao {
	
	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;
	
	@Override
	public boolean saveOrder(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_order(shop_id, o_insideorder, u_id, p_id, p_amount, p_price,"
					+ " p_sale, p_total, o_createdatetime) values(?,?,?,?,?,?,?,?,now())";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("insideorder"));
			ps.setObject(3, map.get("userId"));
			ps.setObject(4, map.get("productId"));
			ps.setObject(5, 1);
			ps.setObject(6, map.get("productPrice"));
			ps.setObject(7, map.get("productSale"));
			ps.setObject(8, map.get("productTotal"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean updateBackOrder(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			Map<String, Object> shopResult = new HashMap<String, Object>();
			Map<String, Object> orderResult = new HashMap<String, Object>();
			qRunner = new QueryRunner();
			//获取商户信息
			String findShopSql = "select shop_id as shopId from tb_shop where s_code=?";
			conn = getConn();
			shopResult = qRunner.query(conn, findShopSql, new MapHandler(), map.get("mchId"));
			
			//获取订单信息
			String findOrderSql = "select * from tb_order where shop_id=? and o_insideorder=?";
			orderResult = qRunner.query(conn, findOrderSql, new MapHandler(), shopResult.get("shopId"), map.get("insideorder"));
			map.put("shopId", shopResult.get("shopId").toString());
			if(null != orderResult.get("o_outsideorder")) {
				return f;
			}
			
			//修改订单状态
			String updateOrderSql = "update tb_order set o_outsideorder=?, o_paymentdatetime=?, o_status=1, o_payment=0 where shop_id=? and o_insideorder=?";
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(updateOrderSql);
			ps.setObject(1, map.get("outsideorder"));
			ps.setObject(2, map.get("datetime"));
			ps.setObject(3, shopResult.get("shopId"));
			ps.setObject(4, map.get("insideorder"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public Map<String, Object> getOrderByInsider(Map<String, String> map) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			qRunner = new QueryRunner();
			//获取订单信息
			String findOrderSql = "select * from tb_order where shop_id=? and o_insideorder=?";
			conn = getConn();
			result = qRunner.query(conn, findOrderSql, new MapHandler(), map.get("shopId"), map.get("insideorder"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}
	
	
}
